package com.ray.server;

import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.HashMap;

import com.ray.basic.BASIC;

public class Udp_in implements Runnable{
	private int servPort=BASIC.get_servPort();
	private static HashMap<String , SocketAddress> map_remote= new HashMap<String, SocketAddress>();
	private DatagramSocket ds=null;
	private DatagramPacket packet=null;
	public void run() {
		try {
			ds = new DatagramSocket(servPort);
			byte[] buffer = new byte[4];
			packet = new DatagramPacket(buffer, buffer.length);
		} catch (SocketException e1) {
			e1.printStackTrace();
		}
		while (true){
				try {
					ds.receive(packet);
					byte[] SingePkg= packet.getData();
					String username=SingePkg.toString();
					SocketAddress remote=packet.getSocketAddress();
//					System.out.println(remote);
					map_remote.put(username, remote);
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	public static HashMap<String , SocketAddress> get_map_remote(){
		return map_remote;
	}
}
